******************************************************
******************************************************
* Program that defines the restricted samples
* This new version uses as reference the fourth month
* of the sample instead of the second
******************************************************
******************************************************

*Brochu
* minimum wage project
* LFS

* program name: panel_1996_2012.do
* created: June 9, 2014
* updated: JUne 17, 2014
* modified: July, 2015
* Modified: April 7, 2017.
* Modified: April 3, 2019.


clear
discard

/* set directories */
* note:  no spaces allowed in directory names.


* workdata is the directory that contains the working data sets
local workdata "V:\Townsend_5609\Iain_Feb27\WorkingData"




set more off

set memory 500m
set matsize 600

******************************************************
******************************************************
* creating a restricted sample
******************************************************
******************************************************

use `workdata'\panel_6M_allyears.dta, clear

drop if syear1 == .

di in red "the raw sample of incoming rotation"
sum PROV


**************************************************
* drop if in the military in P1
**************************************************

drop if membstat1==2 

**************************************************
* drop if less than 15 or more than 59
**************************************************

drop if age1<15
drop if age1>59

********************************************************
* drop if no labour force information in Period 1
********************************************************

drop if lfsstat1 == . & syear1 ~= .

********************************************************
* keep employees only. 
********************************************************

keep if lfsstat1 <= 2 & cowmain1 <= 2

********************************************************
********************************************************
********************************************************
* FLAGS
*
* flag if missing info in period 3
* 
* I - Due to full-time member of armed forces, or missing
* labour force statusd information.
*
* II - unbalanced panel
*
* III - full imputation or carrying forward
*
* IV - Odd tenure (two types)
********************************************************
********************************************************
********************************************************

********************************************************
* full-time member of armed forces, or missing LF status
********************************************************

gen 	flagI_missing = 1 if membstat3 == 2
replace flagI_missing = 1 if lfsstat3  == . & syear3 ~= .

********************************************************
* flag if unbalanced (no fourth month) 
********************************************************

gen flagII_unbalanced = 1 if syear3 == .

***********************************************************************
* flag if record is fully imputed in either period (1st or 4th), or 
* carried forward. Given that P1 is the incomning rotation months, 
* there cannot be any carrying forward in P1.
***********************************************************************

gen     flagIII_imputed = 1 if rcdo1 == 3 
replace flagIII_imputed = 1 if rcdo1 == 4 
replace flagIII_imputed = 1 if rcdo1 == 7 
replace flagIII_imputed = 1 if rcdo1 == 8 

replace flagIII_imputed = 1 if rcdo3 == 3 
replace flagIII_imputed = 1 if rcdo3 == 4 
replace flagIII_imputed = 1 if rcdo3 == 7 
replace flagIII_imputed = 1 if rcdo3 == 8 

***************************************************************************
* flag if odd tenure - two type
* IVA - is less restrictive. It allows for the possibility that tenure3=3
* is a noew job
***************************************************************************

gen 	flagIVA_oddtenure = 1 if tenure3 >  tenure1+2 & tenure1~= . & tenure3 ~= . 
replace flagIVA_oddtenure = 1 if tenure3 <= tenure1   & tenure3>= 4 & tenure1 ~= . & tenure3 ~= .

gen     flagIVB_oddtenure = 1 if tenure3 >  tenure1+2 & tenure1~= . & tenure3 ~= . 
replace flagIVB_oddtenure = 1 if tenure3 <= tenure1   & tenure3>= 3 & tenure1 ~= . & tenure3 ~= .

****

gen 	flag_restrictionsA=1 if flagI_missing==1 
replace flag_restrictionsA=1 if flagII_unbalanced==1
replace flag_restrictionsA=1 if flagIII_imputed==1
replace flag_restrictionsA=1 if flagIVA_oddtenure==1

gen 	flag_restrictionsB=1 if flagI_missing==1 
replace flag_restrictionsB=1 if flagII_unbalanced==1
replace flag_restrictionsB=1 if flagIII_imputed==1
replace flag_restrictionsB=1 if flagIVB_oddtenure==1

*********************************************************
*********************************************************
* creating variables 
*********************************************************
*********************************************************

* female dummy
gen female=0 if sex1==1
replace female=1 if sex1==2
drop sex1

* age
gen age=age1
drop age1

* education
gen educlev=educlev1
drop educlev1

* province of residence
gen prov=prov1
drop prov1

* wage in dollars
gen wage1=hrlyearn1/100 if hrlyearn1~=. & year<=2012
replace wage1=hrlyearn1 if hrlyearn1~=. & year>2012
drop hrlyearn1

gen wage2=hrlyearn2/100 if hrlyearn2~=. & year<=2012
replace wage2=hrlyearn2 if hrlyearn2~=. & year>2012
drop hrlyearn2

gen wage3=hrlyearn3/100 if hrlyearn3~=. & year<=2012
replace wage3=hrlyearn3 if hrlyearn3~=. & year>2012
drop hrlyearn3

gen wage4=hrlyearn4/100 if hrlyearn4~=. & year<=2012
replace wage4=hrlyearn4 if hrlyearn4~=. & year>2012
drop hrlyearn4

* Dummy for whether paid hourly
gen 	hourlywork1=1 if hrlywork1==1
replace hourlywork1=0 if hrlywork1==2

gen 	hourlywork2=1 if hrlywork2==1
replace hourlywork2=0 if hrlywork2==2

gen 	hourlywork3=1 if hrlywork3==1
replace hourlywork3=0 if hrlywork3==2

gen 	hourlywork4=1 if hrlywork4==1
replace hourlywork4=0 if hrlywork4==2
drop hrlywork1 hrlywork2 hrlywork3 hrlywork4

* Dummy for whether usual receives tips or commissions.
replace tips1=0 if tips1==2
replace tips2=0 if tips2==2
replace tips3=0 if tips3==2
replace tips4=0 if tips4==2

* Binary variable for period 4

gen 	samejob3=0 if syear3~=.
replace samejob3=1 if tenure3==tenure1+2 & syear3~=.

gen 	newjob3=0 if syear3~=.
replace newjob3=1 if tenure3<=3 & syear3 ~=. 
replace newjob3=0 if tenure1==1 & tenure3==3

gen 	nojob3=0 if syear3~=.
replace nojob3=1 if tenure3==. & syear3~=.

gen work3=samejob3+newjob3

********************************************************
********************************************************

keep age female educlev prov /*
*/ syear1 smth1 finalwt1 lfsstat1 wage1 tenure1 cowmain1 wage1 hourlywork1 tips1 /*
*/ syear2 smth2 finalwt2 lfsstat2 wage2 tenure2 cowmain2 wage2 hourlywork2 tips2 /*
*/ syear3 smth3 finalwt3 lfsstat3 wage3 tenure3 cowmain3 wage3 hourlywork3 tips3 /*
*/ syear4 smth4 finalwt4 lfsstat4 wage4 tenure4 cowmain4 wage4 hourlywork4 tips4 samejob3 newjob3 nojob3 work3 /*
*/ flagI_missing flagII_unbalanced flagIII_imputed flagIVA_oddtenure flagIVB_oddtenure flag_restrictionsA flag_restrictionsB why*

/*
sum
sum flagI_missing flagII_unbalanced flagIII_imputed flagIVA_oddtenure flagIVB_oddtenure flag_restrictionsA flag_restrictionsB
sum syear1 smth1 finalwt1 age female educlev prov lfsstat1 tenure1 cowmain1 wage1 hourlywork1 tips1 
sum syear3 smth3 finalwt3 age female educlev prov lfsstat3 tenure3 cowmain3 wage3 hourlywork3 tips3 samejob3 newjob3 nojob3 work3 if lfsstat3 <= 2 
sum syear3 smth3 finalwt3 age female educlev prov lfsstat3 tenure3 cowmain3 wage3 hourlywork3 tips3 samejob3 newjob3 nojob3 work3 if lfsstat3 <= 2 & cowmain3 <= 2

sum syear1 smth1 finalwt1 age female educlev prov lfsstat1 tenure1 cowmain1 wage1 hourlywork1 tips1 if flag_restrictionsA~=1
sum syear3 smth3 finalwt3 age female educlev prov lfsstat3 tenure3 cowmain3 wage3 hourlywork3 tips3 samejob3 newjob3 nojob3 work3 if flag_restrictionsA~=1
sum syear3 smth3 finalwt3 age female educlev prov lfsstat3 tenure3 cowmain3 wage3 hourlywork3 tips3 samejob3 newjob3 nojob3 work3 if lfsstat3<=2 & flag_restrictionsA~=1
sum syear3 smth3 finalwt3 age female educlev prov lfsstat3 tenure3 cowmain3 wage3 hourlywork3 tips3 samejob3 newjob3 nojob3 work3 if lfsstat3<=2 & cowmain3<=2 & flag_restrictionsA~=1

sum syear1 smth1 finalwt1 age female educlev prov lfsstat1 tenure1 cowmain1 wage1 hourlywork1 tips1 if flag_restrictionsB~=1
sum syear3 smth3 finalwt3 age female educlev prov lfsstat3 tenure3 cowmain3 wage3 hourlywork3 tips3 samejob3 newjob3 nojob3 work3 if flag_restrictionsB~=1
sum syear3 smth3 finalwt3 age female educlev prov lfsstat3 tenure3 cowmain3 wage3 hourlywork3 tips3 samejob3 newjob3 nojob3 work3 if lfsstat3<=2 & flag_restrictionsB~=1
sum syear3 smth3 finalwt3 age female educlev prov lfsstat3 tenure3 cowmain3 wage3 hourlywork3 tips3 samejob3 newjob3 nojob3 work3 if lfsstat3<=2 & cowmain3<=2 & flag_restrictionsB~=1
*/

drop if wage1==.






order age female educlev prov /*
*/ syear1 smth1 finalwt1 lfsstat1 wage1 tenure1 cowmain1 wage1 hourlywork1 tips1 /*
*/ syear2 smth2 finalwt2 lfsstat2 wage2 tenure2 cowmain2 wage2 hourlywork2 tips2 /*
*/ syear3 smth3 finalwt3 lfsstat3 wage3 tenure3 cowmain3 wage3 hourlywork3 tips3 /*
*/ syear4 smth4 finalwt4 lfsstat4 wage4 tenure4 cowmain4 wage4 hourlywork4 tips4 samejob3 newjob3 nojob3 work3 /*
*/ flagI_missing flagII_unbalanced flagIII_imputed flagIVA_oddtenure flagIVB_oddtenure flag_restrictionsA flag_restrictionsB  why*

save `workdata'\panel_6M_sample.dta, replace
